﻿cmake_minimum_required(VERSION 2.10)
project(protobuf-demo)

# Find required protobuf package
#find_package(protobuf CONFIG REQUIRED)


set(MSG_SRC "" )
set(MSG_HDRS "")
# Generate message project.
add_subdirectory(message)

set_source_files_properties(${MSG_SRC} ${MSG_HDRS} PROPERTIES GENERATED TRUE)

source_group("Generated" FILES ${MSG_SRC} ${MSG_HDRS})

message(status "     Protobuf_SRC_ROOT_FOLDER   " ${Protobuf_SRC_ROOT_FOLDER})

message(status "     absl_DIR   " ${absl_DIR})
find_package(absl CONFIG REQUIRED)

message(status "     protobuf_DIR   " ${protobuf_DIR})
find_package(protobuf CONFIG REQUIRED)

message(status "     CMAKE_MSVC_RUNTIME_LIBRARY   " ${CMAKE_MSVC_RUNTIME_LIBRARY})

# set runtime-library according to protobuf build type.
if (protobuf_BUILD_SHARED_LIBS)
  set(protobuf_SHARED_OR_STATIC "SHARED")
else (protobuf_BUILD_SHARED_LIBS)
  set(protobuf_SHARED_OR_STATIC "STATIC")
  # The CMAKE_<LANG>_FLAGS(_<BUILD_TYPE>)? is meant to be user controlled.
  # Prior to CMake 3.15, the MSVC runtime library was pushed into the same flags
  # making programmatic control difficult.  Prefer the functionality in newer
  # CMake versions when available.
  if(${CMAKE_VERSION} VERSION_GREATER 3.15 OR ${CMAKE_VERSION} VERSION_EQUAL 3.15)
    if (protobuf_MSVC_STATIC_RUNTIME)
        set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)
    else()
        set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
    endif()
  else()
    # In case we are building static libraries, link also the runtime library statically
    # so that MSVCR*.DLL is not required at runtime.
    # https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
    # This is achieved by replacing msvc option /MD with /MT and /MDd with /MTd
    # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
    if (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
      foreach(flag_var
          CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
        if(${flag_var} MATCHES "/MD")
          string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
        endif(${flag_var} MATCHES "/MD")
      endforeach(flag_var)
    endif (MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  endif()
endif (protobuf_BUILD_SHARED_LIBS)
message(status "     CMAKE_MSVC_RUNTIME_LIBRARY   " ${CMAKE_MSVC_RUNTIME_LIBRARY})

message(status "     protobuf_BUILD_SHARED_LIBS   " ${protobuf_BUILD_SHARED_LIBS})

set(executable_name protobuf-demo)
add_executable(${executable_name} main.cpp ${MSG_SRC} ${MSG_HDRS})
target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIR} PUBLIC ${Protobuf_SRC_ROOT_FOLDER} ${PROJECT_BINARY_DIR})
target_link_libraries(${executable_name} protobuf::libprotobuf)


#message(status "   protobuf::libprotobuf:"  "protobuf::libprotobuf")
#message(status "   protobuf_ABSL_USED_TARGETS:" ${protobuf_ABSL_USED_TARGETS})